-- to_monster_skill
-- create by cheny3 Nov/19/2016
-- 给怪物增加技能

return {
    apply = function(source, target, skillId, round, para, extra)
        if source.type ~= OBJECT_TYPE_MONSTER then
            return {};
        end

        -- 场上活着的怪物
        local targets = {};
        local grids = DungeonM.getCurrentDungeon();
        for _, grid in ipairs(grids) do
            if  grid:isMonster() and
                grid:isOpened() and
                not grid.monster:isDead() and
                grid.monster:query("base_class_id") == para[1] then
                table.insert(targets, grid.monster);
            end
        end

        -- 每个怪物
        local applySkill;
        for _, target in ipairs(targets) do
            if type(para[2]) == "number" then
                applySkill = para[2];
            elseif type(para[2]) == "string" then
                applySkill = FormulaM.invoke(para[2], ME.user, target);
            end

            if type(applySkill) == "number" then
                SkillM.apply(target, target, applySkill, round);
                EventMgr.fire(event.TO_MONSTER_SKILL, {["baseClassId"] = para[1], ["pos"] = target:getPos(), ["skillId"] = para[2], })
            end
        end

        -- 返回作用目标
        return targets;
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), arr[2], };
    end,
};
